package com.bsj.power.sdk.web.api;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.power.common.constant.MqTopicAndTagConstants;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.mq.sdk.SdkAnnex;
import com.bsj.power.common.def.mq.sdk.face.PersonInfo;
import com.bsj.power.common.util.SpringUtil;
import com.bsj.power.sdk.config.hksdk.user.UserManage;
import com.bsj.power.sdk.config.hksdk.video.VideoPlayOp;
import com.bsj.power.sdk.util.VideoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yinhao
 * @version 1.0
 * @description 测试
 * @date 2024/8/23
 */
@Slf4j
@RestController
@RequestMapping("/test/")
public class TestApi {

    @Autowired
    private BaseMqPushUtil mqPushUtil;

    @SaIgnore
    @RequestMapping("mq")
    public JsonResult doLogin(@RequestBody SdkAnnex sdkAnnex) {
        mqPushUtil.sendTopicBytes(MqTopicAndTagConstants.SDK_FILE_SERVER_TOPIC,
                MqTopicAndTagConstants.SDK_FILE_DOWNLOAD_TAG, JSON.toJSONBytes(sdkAnnex));
        return JsonResult.fail("登录失败");
    }

    @SaIgnore
    @RequestMapping("convert")
    public JsonResult doConvert() throws Exception {
        VideoConvertUtil.convert("/root/soft/module-sdk/1122334455.h264",
                "/root/soft/module-sdk/1122.mp4");
        return JsonResult.success();
    }

    @SaIgnore
    @RequestMapping("realVideoPlay")
    public JsonResult realVideoPlay(Integer userID, Integer iChannelNo) throws Exception {
        VideoPlayOp.realPlay(userID, iChannelNo, "D:\\Program Files", "\\hksdk\\" + System.currentTimeMillis() + ".jpg");
        return JsonResult.success();
    }

//    @SaIgnore
//    @GetMapping("getPersonInfo")
//    public JsonResult getPersonInfo(Integer userID) throws Exception {
//        String result = UserManage.searchUserInfo(userID);
//        if (StringUtils.isBlank(result)) {
//            return JsonResult.fail("返回结果为空");
//        } else {
//            return JsonResult.success(result);
//        }
//    }
//
//    @SaIgnore
//    @GetMapping("getPersonInfoByPersonNo")
//    public JsonResult getPersonInfoByPersonNo(Integer userID, String personNo) throws Exception {
//        String result = UserManage.searchUserInfoByPersonNo(userID, personNo);
//        if (StringUtils.isBlank(result)) {
//            return JsonResult.fail("返回结果为空");
//        } else {
//            return JsonResult.success(result);
//        }
//    }

    @SaIgnore
    @GetMapping("getUserByEmployeeNo")
    public JsonResult getUserByEmployeeNo(Integer userID, String personNo) throws Exception {
        String result = UserManage.getUserByEmployeeNo(userID, personNo);
        if (StringUtils.isBlank(result)) {
            return JsonResult.fail("返回结果为空");
        } else {
            //解析数据
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject userInfoSearch = jsonObject.getJSONObject("UserInfoSearch");
            JSONArray jsonArray = userInfoSearch.getJSONArray("UserInfo");
            if (!CollectionUtils.isEmpty(jsonArray)) {
                JSONObject jsonData = (JSONObject) jsonArray.get(NumericalConstants.ZERO);
                jsonData.getString("employeeNo");
                jsonData.getString("name");
                return JsonResult.success(jsonData);
            } else {
                return JsonResult.fail("解析的数据为空");
            }
        }
    }

    @SaIgnore
    @GetMapping("/faceDataTest")
    public JsonResult faceDataTest(String srcIndex, String extEventPersonNo, String name) throws Exception {
        PersonInfo personInfo = new PersonInfo(extEventPersonNo, name);
        personInfo.setSrcIndex("DS-SDT007-BC20231018V031000CHAY4584264");
        BaseMqPushUtil baseMqPushUtil = SpringUtil.getBean(BaseMqPushUtil.class);
        baseMqPushUtil.sendTopicBytes(MqTopicAndTagConstants.BASE_SERVER_PLS_FACE_TOPIC,
                MqTopicAndTagConstants.BASE_CHANGE_PLS_FACE_TAG, JSON.toJSONBytes(personInfo));
        log.info("mq消息已发送");
        return JsonResult.success();
    }

}
